/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package DAO;

import Classes.UniMedidaClass;
import java.util.List;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;

/**
 *
 * @author BRUNA
 */
public class UniMedidaDAO {
    private Session session;
    
    public UniMedidaDAO(){
            session = Util.HibernateUtil.getSessionFactory().openSession(); 
            }
    
    public void insert(UniMedidaClass uni){
        Transaction t = session.beginTransaction();
        session.save(uni);
        t.commit();
        }
    
    public void update(UniMedidaClass uni){
        Transaction t = session.beginTransaction();
        session.merge(uni);
        t.commit();
        }
    
    public void delete(UniMedidaClass uni){
        Transaction t = session.beginTransaction();
        session.delete(uni);
        t.commit();
        }
    
    //buscar uma unidade de medida
    public UniMedidaClass findbyid(int id){
        return (UniMedidaClass)session.load(UniMedidaClass.class, id);
        }
    
    //Buscar todas as unidades de medida
    public List<UniMedidaClass> findall(){
        Query qu = session.createQuery("From UniMedidaClass");
        return qu.list();
        }
    
    public List<UniMedidaClass>findAllUni(String uni){
        Query qu = session.createQuery("From UniMedidaClass where descricao like '%"+uni+"%'");
        return qu.list();
    }
    
    public List<UniMedidaClass> findByHQL (String hql, List params){
        Query q = this.session.createQuery(hql);
        
        int i = 0;
        for (Object ps: params){
            q.setParameter(i++, ps);            
        }
        return q.list();
        
    }
}
